package com.hundsun.yr.universal.p2p.ui.activity;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.common.PackageUtils;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseMineWebActivity;
import com.hundsun.yr.universal.p2p.utils.KeyBoardListener;

import de.greenrobot.event.EventBus;


/**
 * @version V1.0
 * @Title: WebPayActivity.java
 * @Description: 支付WEB--支付页面h5
 * @date 2016-3-16 下午7:14:15
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class WebPayActivity extends BaseMineWebActivity implements OnClickListener {

    private boolean isSuccess = false;

    public HSSharedPreferences sharedPreferences = null;
    /**
     * 跳转标识
     */
    public String NAVIGATE_TAG;
    private String INVESTAMOUNT;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {

        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        NAVIGATE_TAG = extras.getString(Extras.NAVIGATE_TAG);
        INVESTAMOUNT = extras.getString("INVESTAMOUNT");
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }


    @Override
    protected void initViewsAndEvents() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        KeyBoardListener.getInstance(this).init();
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
        toolbar_menu.setText(R.string.complete);
        toolbar_menu.setTextColor(getResources().getColor(R.color.white));
        toolbar_menu.setVisibility(View.GONE);
        toolbar_menu.setOnClickListener(this);
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
        mBrowserLayout.mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                Log.e("ddd",url);

                return super.shouldInterceptRequest(view, url);
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains("zwgt.com")) {
                    syncCookie(mContext, url, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), PackageUtils.getVersionName(mContext));
                }

                if (url.contains(getString(R.string.login_successful2))){
                    readyGoThenKill(UpdatePayPwdActivity.class);
                    return false;
                }
                else if (url.contains("zwgt.com") && url.contains("retCode=00000000")) {
                    isSuccess = true;
                    //投资成功跳回主页
                    finish();
                    showToast(getString(R.string.mine_pay_success));
                    EventBus.getDefault().post(new EventCenter(Extras.INVEST_REFRESH));//登陆成功通知某些页面进行数据刷新
                    EventBus.getDefault().post(new EventCenter(Constants.EVENT_REFRESH_DATA));//登陆成功通知某些页面进行数据刷新
                    //跳转至我的投资，投标中
                    if (TextUtils.equals(NAVIGATE_TAG,"INVESTLIST")){
                        if (BaseAppManager.getInstance().getActivity(WaitPayDetailActivity.class) != null){
                            BaseAppManager.getInstance().getActivity(WaitPayDetailActivity.class).finish();
                        }

                        if (BaseAppManager.getInstance().getActivity(MineInvestActivity.class) != null){
                            BaseAppManager.getInstance().getActivity(MineInvestActivity.class).finish();
                        }
                    } else {
//                        readyGoThenKill(MineInvestActivity.class);
                        //投资成功跳转至投资成功页面
                        Bundle bundle = new Bundle();
                        bundle.putString("INVESTAMOUNT",INVESTAMOUNT);
                        readyGoThenKill(InvestSuccessAcitivity.class,bundle);

                    }

                } else{
                    mBrowserLayout.mWebView.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void finish() {
        super.finish();
        if (!isSuccess){
            EventBus.getDefault().post(new EventCenter(Extras.INVEST_REFRESH));//登陆成功通知某些页面进行数据刷新
        }
    }
}
